//
//  DataStatisticsController.m
//  tuanGG
//
//  Created by 微他 on 15/9/21.
//  Copyright (c) 2015年 weita. All rights reserved.
//

#import "DataStatisticsController.h"
#import "Header.h"
#import "DataStatisticsTableViewCell.h"
@interface DataStatisticsController ()<UITableViewDataSource,UITableViewDelegate>
@property (strong,nonatomic)UISegmentedControl *segmentrol;
@property (strong,nonatomic)UIView *line ;
@property (strong,nonatomic)UITableView *tableView;
@property (strong,nonatomic)UIView *lineView;
@property (nonatomic)BOOL mode;
@property (strong,nonatomic)NSString *string;
@property (strong,nonatomic)NSArray *dataArray;
@property (strong,nonatomic)UITextField *textfield;
@property (strong,nonatomic)NSString *pirceString;
@property (strong,nonatomic)UILabel *price;
@property (assign,nonatomic)NSInteger date;
@end

@implementation DataStatisticsController

- (void)viewWillAppear:(BOOL)animated{
    [self tabBarShow];
    [self Networking];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithWhite:0.800 alpha:1.000];
    [self addnavigationTitle:@"数据统计" navColor:@"4fd2be"];
    [self initlizeDataSource];
    [self initView];
    [self addView];
    [self addTableView];
}
- (void)initlizeDataSource {
    _dataArray = [NSArray array];
    _string = @"day";
    _date = 1;
    _mode = NO;
}
- (void)initView{
    NSArray *array = @[@"天",@"月"];
    _segmentrol = [[UISegmentedControl alloc]initWithItems:array];
    _segmentrol.frame = CGRectMake(0, 0, ScreenWidth, 45) ;
    _segmentrol.tintColor = [UIColor clearColor];
    _segmentrol.selectedSegmentIndex = 0;
    _segmentrol.backgroundColor =[UIColor whiteColor];
    [self.view addSubview:_segmentrol];
    //选中状态
    NSDictionary *selectedTextttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:18],NSForegroundColorAttributeName:[UIColor colorWithHexString:@"969696"]};
    [_segmentrol setTitleTextAttributes:selectedTextttributes forState:UIControlStateSelected];
    NSDictionary *unselectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:18],
                                               NSForegroundColorAttributeName: [UIColor colorWithHexString:@"969696"]};
    [_segmentrol setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];
    [self.view addSubview:_segmentrol];
    [_segmentrol addTarget:self action:@selector(dateButton:) forControlEvents:UIControlEventValueChanged];
    
    _line = [[UIView alloc]initWithFrame:CGRectMake(0, 44, ScreenWidth/2, 2)];
    _line.backgroundColor = [UIColor colorWithHexString:@"4fd2be"];
    //分割线
    [_segmentrol addSubview:_line];
    
    UIView *lines = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 1, _segmentrol.frame.size.height-10)];
    lines.backgroundColor = [UIColor blackColor];
    lines.alpha = 0.1;
    lines.center = CGPointMake(ScreenWidth/2, _segmentrol.frame.size.height/2);
    [_segmentrol addSubview:lines];
    
    _textfield = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 40)];
    _textfield.backgroundColor = [UIColor redColor];
    
}
- (void)addView {
    _lineView = [[UIView alloc]initWithFrame:CGRectMake(0, _segmentrol.frame.size.height+1, ScreenWidth, 100)];
    _lineView.backgroundColor = [UIColor whiteColor];

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, ScreenWidth, 20)];
    label.text = @"平均收益(元)";
    label.font = [UIFont systemFontOfSize:15];
    label.textColor = [UIColor colorWithHexString:@"#989898"];
    label.textAlignment = NSTextAlignmentCenter;
    [_lineView addSubview:label];
    
    _price = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(label.frame)+20, ScreenWidth, 20)];
    _price.textColor = [UIColor colorWithHexString:@"ff5500"];
    _price.font = [UIFont fontWithName:font_Font size:20];
    _price.textAlignment = NSTextAlignmentCenter;
    _price.text = @"0.0";
    [_lineView addSubview:_price];
    
    
    
    
    for (int i = 0 ; i < 2; i++ ) {
        UIView *Total = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth-20, 60)];
        Total.center  =CGPointMake(ScreenWidth/2,Total.frame.size.height/2+10+i*(Total.frame.size.height+10));
        Total.backgroundColor = [UIColor colorWithHexString:@"4fd2be"];
//        [_lineView addSubview:Total];
        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(10, 20, 15, 20)];
        UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(image.frame.size.width+20, Total.frame.size.height/3, 100, 20)];
        nameLabel.textColor = [UIColor whiteColor];
        nameLabel.font = [UIFont systemFontOfSize:14];
        UILabel *number = [[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth-60-image.frame.size.width-nameLabel.frame.size.width-20, Total.frame.size.height/3, Total.frame.size.width-image.frame.size.width-nameLabel.frame.size.width-20, 20)];
        number.textAlignment = NSTextAlignmentRight;
        number.textColor = [UIColor whiteColor];
        number.font = [UIFont systemFontOfSize:14];
        [Total addSubview:image];
        [Total addSubview:nameLabel];
        [Total addSubview:number];
        if (i == 0) {
            image.image = [UIImage imageNamed:@"订单总数"];
            nameLabel.text = @"订单总数(数量)";
            number.text = @"0";
        }else if (i == 1){
            image.image = [UIImage imageNamed:@"收入总数"];
            nameLabel.text = @"收入总数(元)";
            number.text = @"0";
        }
    }
    
}
- (void)addTableView {
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(_segmentrol.frame)+1, ScreenWidth, ScreenHeight-CGRectGetHeight(_segmentrol.frame)-115) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.bounces = NO;
    _tableView.showsVerticalScrollIndicator = NO;
    [_tableView registerClass:[DataStatisticsTableViewCell class] forCellReuseIdentifier:@"CELL"];
    _tableView.tableHeaderView = _lineView;
    [self.view addSubview:_tableView];

}

#pragma mark - 事件实现
- (void)dateButton:(UISegmentedControl *)sender {
    _mode = YES;
    _date = 1;
    if (sender.selectedSegmentIndex == 0) {
        [self oneDay];
        _date = 1;
        _string = @"day";
        [self Networking];
    }else if (sender.selectedSegmentIndex == 1){
        [self oneMonth];
        _date = 0;
        _string = @"month";
        [self Networking];
    }
        
}
- (void)oneDay {

    //一天
    [UIView animateWithDuration:0.2 animations:^{
        _line.frame = CGRectMake(0, 44, ScreenWidth / 2, 2);
    }];
    
    
}

- (void)oneMonth{
    //一月
    [UIView animateWithDuration:0.2 animations:^{
        _line.frame = CGRectMake(ScreenWidth/2, 44, ScreenWidth/2, 2);
    }];
    
}
#pragma mark -  UITableViewDataSource
- (void)headLabel {

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
     NSLog(@"%ld",(unsigned long)_dataArray.count);
    return  _dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DataStatisticsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
    if (indexPath.section == 0) {
        cell.textfield.backgroundColor = [UIColor colorWithHexString:@"ff5500"];
    }else{
        cell.textfield.backgroundColor = [UIColor colorWithWhite:0.800 alpha:1.000];
    }
    NSDate *currentDate = [NSDate date];//获取当前时间，日期
    NSTimeInterval oneDay = 0;
    NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
    if (_date == 0) {
        oneDay = 24*60*60*(indexPath.section*30);
        [dateFormatter1 setDateFormat:@"YYYY－MM"];
    }else {
        oneDay = 24*60*60*(indexPath.section+1);
        [dateFormatter1 setDateFormat:@"YYYY－MM－dd"];
    }
    NSDate *theDate  =[currentDate initWithTimeIntervalSinceNow:-oneDay];
    NSString *dateString1 = [dateFormatter1 stringFromDate:theDate];
    NSLog(@"dateString:%@",dateString1);
    cell.textfield.text = [NSString stringWithFormat:@"%@",dateString1];
    if ([_pirceString integerValue] != 0) {
        cell.textfield.frame = CGRectMake(0, 0, ScreenWidth/2+[_dataArray[indexPath.section][@"price"]integerValue]/50,40);
        cell.label.text = [NSString stringWithFormat:@"%.1f",[_dataArray[indexPath.section][@"price"]floatValue]];
    }else {
        cell.textfield.frame = CGRectMake(0, 0, ScreenWidth/2+[_dataArray[indexPath.section]integerValue]/50,40);
        cell.label.text = [NSString stringWithFormat:@"%.1f",[_dataArray[indexPath.section]floatValue]];
    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

#pragma mark - 网络请求
- (void)Networking {
    NSDictionary *dictionary = @{@"type":_string};
    [GLAFRequest startRequest:data_url paramsDic:dictionary defaultdialog:YES ispost:NO handleSuccess:^(id sta, NSDictionary *paramsDic) {
        NSLog(@"%@",sta);
        if ([sta[@"code"]integerValue] == 1) {
            _dataArray = [sta[@"iteminfo"] copy];
            _pirceString = [sta[@"incomesum"]copy];
            _price.text = [NSString stringWithFormat:@"%@",sta[@"incomesum"]];
            [_tableView reloadData];
        }
    } doFinally:^(NSDictionary *paramsDic) {

    }];
}
@end
